<template>
  <div>
    <template v-if="meta">
      <el-input size="small" clearable show-word-limit placeholder="简单描述下增量" v-model="title"  maxlength="50" />

      <p>源文件数量: {{meta.sourceFileCount}} 编译后文件数量: {{meta.binFileCount}} 删除文件数量: {{meta.deleteFileCount}}
        <template v-if="meta.errorFileCount">
          错误文件数量:
          <span class="text-danger">{{meta.errorFileCount}}</span>
        </template>
        <el-button class="margin-left" size="small" type="text" @click="downloadTarFile" icon="el-icon-download" >下载</el-button>
      </p>

      <template v-if="meta.compileFileInfos && meta.compileFileInfos.length > 0">
        <div class="margin-bottom">编译文件列表</div>
        <el-table
          v-loading="loading"
          :data="meta.compileFileInfos"
          border
          stripe
          size="mini"
          height="500"
          :row-class-name	="rowStyle"
        >
          <el-table-column type="expand" >
            <template slot-scope="{row}">
              <ul style="list-style-type: none;line-height: 1.5;">
                <li v-for="compileFile in row.compileFiles">
                  <span class="margin-right">{{formatDuration(new Date().getTime() - compileFile.lastedModifyTime)}}</span>
                  <span >{{compileFile.path}}</span>
                </li>
              </ul>
            </template>
          </el-table-column>

          <el-table-column type="index" width="50"/>
          <el-table-column prop="diffFilePath.changeType" label="变更类型" width="100"/>
          <el-table-column label="cc" width="55">
            <template slot-scope="{row}">
              <span v-if="row.diffFilePath.changeType !== 'DELETE' && row.compileFiles.length === 0" class="text-danger">
                {{row.compileFiles.length}}
              </span>
              <span v-else>{{row.compileFiles.length}}</span>
            </template>
          </el-table-column>
          <el-table-column  label="变更时间" width="150">
            <template slot-scope="{row}">{{ row.diffFilePath.relativeFile.lastedModifyTime ? formatDuration(new Date().getTime() - row.diffFilePath.relativeFile.lastedModifyTime) : null}}</template>
          </el-table-column>
          <el-table-column prop="diffFilePath.relativeFile.path" label="文件路径" />

        </el-table>
      </template>

    </template>

  </div>
</template>

<script>
import core from '@/api/core'
import vcs from '@/api/vcs'
import { formatTime,formatDuration } from '@/utils'

export default {
  name: 'binFileMeta',
  props:{
    meta: {
      type: Object,
      default: null
    },
    params: {
      type: Object,
      default: null
    }
  },
  data(){
    return {
      loading: false,
      title: null
    }
  },
  methods: {
    formatTime,formatDuration,
    downloadTarFile(){
      let params = Object.assign({title: this.title},this.params)
      vcs.project.downloadBinFiles(params).then(res => {
        core.fileDownload(res.data.relativePath,(this.title ? this.title : new Date().toLocaleString())+".zip")
      })

    },
    rowStyle({row, rowIndex}){
      if (row.diffFilePath.changeType !== 'DELETE' && row.compileFiles.length === 0){
        return 'text-danger'
      }
      return null
    }
  }
}
</script>

<style scoped>

</style>
